﻿using System;
using Framework.MVC;
using Account.Model;
using Account.Model.Behavior;
using Identity.Model;
using Framework.MVVM.Model;
using Common.IService;
using Common.Service;
using Log.IService;
using Identity.IService;
using Identity.Service;
using Account.IService;
using Account.Service;

namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IVerifyCodeService), typeof(VerifyCodeService), "Instance")]
    [ServiceAttribute(typeof(IUserMailService), typeof(UserMailService), "Instance")]
    [ServiceAttribute(typeof(IUserService), typeof(UserService), "Instance")]
    [ServiceAttribute(typeof(IAccountService), typeof(AccountService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class NewAUMModel : AsyncModelBase, IValidateVerifyCode, IDeleteVerifyCode, INewUser, INewUserMail, INewIdentity, INewAccount
    {
        #region Field

        private const string m_verifyvalue_complete = "naccount4";

        private const string m_resource = "vc00";

        private const string m_user_resource = "u000";

        private const string m_identity = "identity";

        private const string m_code = "code";

        private const string m_error = "error";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateVerifyCode(AccountResource, string.Empty, string.Empty);
        }

        #endregion AsyncModelBase

        #region IValidateVerifyCode

        public string Source { get; set; }

        public string VerifyCode { get; set; }

        public string VerifyValue { get; set; }

        public void OnValidateVerifyCodeComplete(string key, bool entry)
        {
            this.DeleteVerifyCode(AccountResource, string.Empty, string.Empty);
        }

        public void OnValidateVerifyCodeError(string key)
        {
            OnComplete(m_error, "verifycode");
            OnEndRequest();
        }

        #endregion IValidateVerifyCode

        #region IDeleteVerifyCode

        public void OnDeleteVerifyCodeComplete(string key, bool result)
        {
            //this.NewUser(string.Empty, string.Empty);
            this.NewAccount(AccountResource, string.Empty, string.Empty);
        }

        public void OnDeleteVerifyCodeError(string key)
        {
            //this.NewUser(string.Empty, string.Empty);
            this.NewAccount(AccountResource, string.Empty, string.Empty);
        }

        #endregion IDeleteVerifyCode

        #region INewAccount

        public string AccountResource { get; set; }

        public string AccountCode { get; set; }

        public string AccountName { get; set; }

        public string AccountPassword { get; set; }

        public string Address { get; set; }

        public string Mark { get; set; }

        public string SourceCode { get; set; }

        public void OnNewAccountComplete(string key, string code)
        {
            OnComplete(m_code, code);
            UserCode = code;
            this.NewUser(string.Empty, string.Empty);
        }

        public void OnNewAccountError(string key)
        {
            OnComplete(m_error, "account");
            OnEndRequest();
        }

        #endregion INewAccount

        #region INewUser

        public string UserCardID { get; set; }

        public string UserCode { get; set; }

        public string UserName { get; set; }

        public void OnNewUserComplete(string key, Account.IService.Entry.UserEntry entry)
        {
            UserMailCode = entry.code;
            this.NewUserMail(string.Empty, string.Empty);
        }

        public void OnNewUserError(string key)
        {
            OnComplete(m_error, "user");
            OnEndRequest();
        }

        #endregion INewUser

        #region INewUserMail

        public string UserMailAddress { get; set; }

        public string UserMailCode { get; set; }

        public void OnNewUserMailComplete(string key, Account.IService.Entry.UserMailEntry entry)
        {
            IdentityResource = m_user_resource;
            IdentityValue = entry.code;
            this.NewIdentity(string.Empty, string.Empty);
        }

        public void OnNewUserMailError(string key)
        {
            OnComplete(m_error, "email");
            OnEndRequest();
        }

        #endregion INewUserMail

        #region INewIdentity

        public string IdentityCode { get; set; }

        public string IdentityResource { get; set; }

        public string IdentityValue { get; set; }

        public void OnNewIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            AccountCode = entry.value;
            OnComplete(m_identity, entry.code);
            OnEndRequest();
        }

        public void OnNewIdentityError(string key)
        {
            OnComplete(m_error, "identity");
            OnEndRequest();
        }

        #endregion INewIdentity 
    }
}